package org.jeecg.modules.demo.mes.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.mes.entity.MesTechnologyProcedureCheckBom;
import org.jeecg.modules.demo.mes.service.IMesTechnologyProcedureCheckBomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 工艺工序检测表
 * @Author: jeecg-boot
 * @Date: 2022-11-01
 * @Version: V1.0
 */
@Api(tags = "工艺工序检测表")
@RestController
@RequestMapping("/mes/mesTechnologyProcedureCheckBom")
@Slf4j
public class MesTechnologyProcedureCheckBomController extends JeecgController<MesTechnologyProcedureCheckBom, IMesTechnologyProcedureCheckBomService> {
    @Autowired
    private IMesTechnologyProcedureCheckBomService mesTechnologyProcedureCheckBomService;

    /**
     * 分页列表查询
     *
     * @param mesTechnologyProcedureCheckBom
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "工艺工序检测表-分页列表查询")
    @ApiOperation(value = "工艺工序检测表-分页列表查询", notes = "工艺工序检测表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<MesTechnologyProcedureCheckBom>> queryPageList(MesTechnologyProcedureCheckBom mesTechnologyProcedureCheckBom,
                                                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                       HttpServletRequest req) {
        QueryWrapper<MesTechnologyProcedureCheckBom> queryWrapper = QueryGenerator.initQueryWrapper(mesTechnologyProcedureCheckBom, req.getParameterMap());
        Page<MesTechnologyProcedureCheckBom> page = new Page<MesTechnologyProcedureCheckBom>(pageNo, pageSize);
        IPage<MesTechnologyProcedureCheckBom> pageList = mesTechnologyProcedureCheckBomService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @ApiOperation(value = "工艺工序检测表-列表查询", notes = "工艺工序检测表-列表查询")
    @GetMapping(value = "/listNoPage")
    public Result<IPage<MesTechnologyProcedureCheckBom>> queryList(MesTechnologyProcedureCheckBom mesTechnologyProcedureCheckBom, HttpServletRequest req) {
        QueryWrapper<MesTechnologyProcedureCheckBom> queryWrapper = QueryGenerator.initQueryWrapper(mesTechnologyProcedureCheckBom, req.getParameterMap());
        Page<MesTechnologyProcedureCheckBom> page = new Page<MesTechnologyProcedureCheckBom>();
        IPage<MesTechnologyProcedureCheckBom> pageList = mesTechnologyProcedureCheckBomService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param mesTechnologyProcedureCheckBom
     * @return
     */
    @AutoLog(value = "工艺工序检测表-添加")
    @ApiOperation(value = "工艺工序检测表-添加", notes = "工艺工序检测表-添加")
    //@RequiresPermissions("org.jeecg.modules.demo:mes_technology_procedure_check_bom:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody MesTechnologyProcedureCheckBom mesTechnologyProcedureCheckBom) {
        //检查检测项是否重复
        checkItem(mesTechnologyProcedureCheckBom);
        mesTechnologyProcedureCheckBomService.save(mesTechnologyProcedureCheckBom);
        return Result.OK("添加成功！");
    }

    private void checkItem(MesTechnologyProcedureCheckBom mesTechnologyProcedureCheckBom) {
        final String checkItem = mesTechnologyProcedureCheckBom.getCheckItem();
        final String procedureId = mesTechnologyProcedureCheckBom.getProcedureId();
        final Map<String, Object> cxMap = new HashMap<>();
        cxMap.put("checkItem", checkItem);
        cxMap.put("procedureId", procedureId);
        final List<MesTechnologyProcedureCheckBom> mesTechnologyProcedureCheckBomList = mesTechnologyProcedureCheckBomService.queryTechnologyProcedureCheckBomListSelective(cxMap);
        if (mesTechnologyProcedureCheckBomList == null || mesTechnologyProcedureCheckBomList.size() == 0) {
            return;
        }
        final String id = mesTechnologyProcedureCheckBom.getId();
        if (StringUtils.isBlank(id)) {
            //新增
            throw new JeecgBootException("检测项重复，请检查后重新填写");
        }
        //编辑
        if (mesTechnologyProcedureCheckBomList.size() > 1) {
            throw new JeecgBootException("检测项重复，请检查后重新填写！");
        }
        if (!StringUtils.equals(id, mesTechnologyProcedureCheckBomList.get(0).getId())) {
            throw new JeecgBootException("检测项重复，请检查后重新填写！！");
        }

    }

    /**
     * 编辑
     *
     * @param mesTechnologyProcedureCheckBom
     * @return
     */
    @AutoLog(value = "工艺工序检测表-编辑")
    @ApiOperation(value = "工艺工序检测表-编辑", notes = "工艺工序检测表-编辑")
    //@RequiresPermissions("org.jeecg.modules.demo:mes_technology_procedure_check_bom:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody MesTechnologyProcedureCheckBom mesTechnologyProcedureCheckBom) {
        mesTechnologyProcedureCheckBomService.updateById(mesTechnologyProcedureCheckBom);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "工艺工序检测表-通过id删除")
    @ApiOperation(value = "工艺工序检测表-通过id删除", notes = "工艺工序检测表-通过id删除")
    //@RequiresPermissions("org.jeecg.modules.demo:mes_technology_procedure_check_bom:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        mesTechnologyProcedureCheckBomService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "工艺工序检测表-批量删除")
    @ApiOperation(value = "工艺工序检测表-批量删除", notes = "工艺工序检测表-批量删除")
    //@RequiresPermissions("org.jeecg.modules.demo:mes_technology_procedure_check_bom:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.mesTechnologyProcedureCheckBomService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "工艺工序检测表-通过id查询")
    @ApiOperation(value = "工艺工序检测表-通过id查询", notes = "工艺工序检测表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<MesTechnologyProcedureCheckBom> queryById(@RequestParam(name = "id", required = true) String id) {
        MesTechnologyProcedureCheckBom mesTechnologyProcedureCheckBom = mesTechnologyProcedureCheckBomService.getById(id);
        if (mesTechnologyProcedureCheckBom == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(mesTechnologyProcedureCheckBom);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param mesTechnologyProcedureCheckBom
     */
    //@RequiresPermissions("org.jeecg.modules.demo:mes_technology_procedure_check_bom:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, MesTechnologyProcedureCheckBom mesTechnologyProcedureCheckBom) {
        return super.exportXls(request, mesTechnologyProcedureCheckBom, MesTechnologyProcedureCheckBom.class, "工艺工序检测表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("mes_technology_procedure_check_bom:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MesTechnologyProcedureCheckBom.class);
    }

}
